package com.wanchuang.qhbb.module.mine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.base.MainApplication;
import com.wanchuang.qhbb.dialog.DialogUtils;
import com.wanchuang.qhbb.help.UserHelper;
import com.wanchuang.qhbb.module.login.LoginActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.utils.APKVersionCodeUtils;
import com.wanchuang.qhbb.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.clear_cache_rlt)
    TextView clearCacheRlt;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.login_out_tv)
    Button loginOutTv;
    @BindView(R.id.version_tv)
    TextView versionTv;
    private boolean isEditSuccess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        StatusBarUtil.setWhiteStatusBar(this);
        ButterKnife.bind(this);
//        cacheSizeTv.setText(CacheUtil.getCacheSize(mContext));
       tv_title.setText("设置");
        versionTv.setText(APKVersionCodeUtils.getVerName(mContext));
    }

    @OnClick({R.id.back_iv, R.id.login_password_tv, R.id.login_out_tv,R.id.pay_password_tv,R.id.clear_cache_rlt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                close();
                break;
            case R.id.login_password_tv: // 安全设置
                Intent intent = new Intent(mContext, ChangePasswordActivity.class);
                intent.putExtra("title", "修改密码");
                startActivity(intent);
                break;
            case R.id.pay_password_tv:  // 支付设置
                Intent intent1 = new Intent(mContext, ChangePayPasswordActivity.class);
                intent1.putExtra("captcha_type", "7");
                startActivity(intent1);
                break;
            case R.id.clear_cache_rlt:
                DialogUtils.showDefaultDialog(mContext, "", "确定清除缓存？", new DialogUtils.DialogImpl() {
                    @Override
                    public void onOk() {
//                        CacheUtil.cleanAllCache(mContext);
                        showToast("清除完毕");
//                        cacheSizeTv.setText(CacheUtil.getCacheSize(mContext));
                    }
                });
                break;
            case R.id.login_out_tv:
                DialogUtils.showDefaultDialog(mContext, "", "确定退出登录?", new DialogUtils.DialogImpl() {
                    @Override
                    public void onOk() {
                        NetworkManager.INSTANCE.post(Apis.logout, new OnRequestCallBack() {
                            @Override
                            public void onOk(byte[] bytes){
                            }
                            @Override
                            public void onError(int code, String errorMessage) {
                            }
                        });
                        Intent intent2 = new Intent(mContext, LoginActivity.class);
                        intent2.putExtra("phone", UserHelper.getUser().getMember_mobile());
                        startActivity(intent2);
                        UserHelper.loginOut();
                        MainApplication.finishAllTempActivity();
                        finish();
                    }
                });
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void close() {
        if (isEditSuccess) setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            isEditSuccess = true;
        }
    }
}
